package com.ktjy;

import io.minio.*;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

@SpringBootTest
public class MinioTester {
    @Test
    public void test(){
        try {
            //根minio服务建立一个连接
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint("http://localhost:9000")
                            .credentials("DKP3TwyZZFCbyENDaY2t", "lPHKfEDXsQFxzM2e7TrWBaTy0he4ft1Lbd6TNWo5")
                            .build();
            //判断桶是否存在
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket("zjgd-crm").build());
            if (!found) {//如果桶不存在
                //创建桶
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("zjgd-crm").build());
            }
            //上传文件
            minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket("zjgd-crm")
                            .object("1.jpg")
                            .filename("F:\\北大青鸟\\T323\\图片\\1.jpg")
                            .build());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test2(){
        try {
            //根minio服务建立一个连接
            MinioClient minioClient =
                    MinioClient.builder()
                            .endpoint("http://localhost:9000")
                            .credentials("DKP3TwyZZFCbyENDaY2t", "lPHKfEDXsQFxzM2e7TrWBaTy0he4ft1Lbd6TNWo5")
                            .build();
            //判断桶是否存在
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket("zjgd-crm").build());
            if (!found) {//如果桶不存在
                //创建桶
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("zjgd-crm").build());
            }
            //上传文件流
            File file=new File("F:\\北大青鸟\\T323\\图片\\2.jpg");
            InputStream in=new FileInputStream(file);

//            minioClient.uploadObject(
//                    UploadObjectArgs.builder()
//                            .bucket("zjgd-crm")
//                            .object("1.jpg")
//                            .filename("F:\\北大青鸟\\T323\\图片\\1.jpg")
//                            .build());
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket("zjgd-crm")
                            .object("2.jpg")
                            .stream(in, file.length(), -1)
                            .contentType("image/jpeg")
                            .build());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
